//
//  MyAlertInputView.m
//  MainProject
//
//  Created by CPX on 2018/7/3.
//  Copyright © 2018年 CPX. All rights reserved.
//

#import "MyAlertInputLocationView.h"
#import "YYCategories.h"

/**
 输入地址弹框
 @author yzl
 */
@interface MyAlertInputLocationView()

@property (nonatomic,strong) NSMutableArray *btns;
@property (nonatomic,strong) UIView *whiteView;
@property (nonatomic,strong) UITextField *textField;
@property (nonatomic,strong) UIView *line;
@property (nonatomic,copy) NSString *placeHolder;

@end

@implementation MyAlertInputLocationView

- (void)initUI{
    [super initUI];
    
    self.whiteView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth - 80, 191)];
    self.whiteView.backgroundColor = [UIColor whiteColor];
    self.whiteView.layer.masksToBounds = YES;
    self.whiteView.layer.cornerRadius = 5;
    self.whiteView.center = self.contentView.center;
    [self.contentView addSubview:self.whiteView];
    
    self.titleLable = [UILabel new];
    self.titleLable.font = [UIFont systemFontOfSize:16];
    self.titleLable.width = self.whiteView.width - 40;
    self.titleLable.numberOfLines = 0;
    self.titleLable.textColor = UIColorHex(#FF6262);
    [self.whiteView addSubview:self.titleLable];
    
    self.line = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.whiteView.width-48, 1)];
    self.line.backgroundColor = UIColorHex(009B47);
    self.textField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, self.whiteView.width-48, 30)];
    
//        self.textField.placeholder = @"Enter the current city";
    self.textField.font = [UIFont systemFontOfSize:15];
    [self.whiteView addSubview:self.textField];
    [self.whiteView addSubview:self.line];
    
//    [self addActionWithTitle:(@"OK") color:UIColorHex(00732F)];
}

+ (instancetype)alertViewWithBlock:(MyAlertInputBlock)block{
    MyAlertInputLocationView *alert = [MyAlertInputLocationView new];
    alert.removeOnTouchOutside = NO;
    alert.titleLable.text = @"Please enter your location!";
    alert.block = block;
    return alert;
}

- (void)addActionWithTitle:(NSString *)title color:(UIColor *)color{
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setTitle:title forState:UIControlStateNormal];
    btn.titleLabel.font = [UIFont systemFontOfSize:14];
    [btn addTarget:self action:@selector(onButtonTouched:) forControlEvents:UIControlEventTouchUpInside];
    [btn setTitleColor:color forState:UIControlStateNormal];
    [self.whiteView addSubview:btn];
    [self.btns addObject:btn];
}

+ (instancetype)alertViewWithBlock:(MyAlertInputBlock)block title:(NSString *)title placeHolder:(NSString *)placeHolder{
    MyAlertInputLocationView *alert = [MyAlertInputLocationView new];
    alert.removeOnTouchOutside = NO;
    alert.titleLable.text = title;
    alert.placeHolder = placeHolder;
    alert.block = block;
    return alert;
}

- (void)layoutSubviews{
    [super layoutSubviews];
    
    CGSize size = [self.titleLable sizeThatFits:CGSizeMake(self.titleLable.width, MAXFLOAT)];
    self.titleLable.height = size.height;
    self.titleLable.top = 20;
    
    self.titleLable.left = 20;
    
    self.line.top = self.titleLable.bottom + 71.5;
    self.textField.bottom = self.line.bottom;
    self.line.centerX = self.titleLable.centerX;
    self.textField.centerX = self.line.centerX;
    
    self.whiteView.height = self.line.bottom + 30 + 36+8;
    self.whiteView.center = self.contentView.center;
    
    CGFloat right = self.titleLable.superview.width - 8;
    CGFloat bottom = self.titleLable.superview.height - 8;
    for (NSInteger i = self.btns.count -1; i>=0; i--) {
        UIButton *btn = self.btns[i];
        btn.size = CGSizeMake(75, 36);
        btn.right = right;
        btn.bottom = bottom;
        right -= 75;
    }
}

- (NSMutableArray*)btns{
    if (!_btns) {
        _btns = [NSMutableArray array];
    }
    return _btns;
}

- (void)onButtonTouched:(UIButton*)sender{
    if (!self.textField.text.length) {
        return;
    }
    if (self.block) {
        self.block(self.textField.text);
    }
    [self removeFromSuperview];
}
@end
